//
//  NSString+additions.m
//  Tool
//
//  Created by ZJ on 17/2/15.
//  Copyright © 2017年 ZJ. All rights reserved.
//

#import "NSString+additions.h"
#define HEIGHT [ [ UIScreen mainScreen ] bounds ].size.height
@implementation NSString (additions)

+ (NSString *)isNilStringReturnString:(NSString *)string{
    return [NSString string:string isNilStringReturnDeftString:@""];
}

+ (NSString *)string:(NSString *)string isNilStringReturnDeftString:(NSString *)Deftstring{
 
    return string ? string : Deftstring;
}

/** 文字自适应 */
+ (CGSize)sizeOfString:(NSString *)string font:(CGFloat)sizefont width:(CGFloat)width {
    
    CGSize size = [string boundingRectWithSize:CGSizeMake(width, [UIScreen mainScreen].bounds.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes: @{NSFontAttributeName:[UIFont systemFontOfSize:sizefont]} context:nil].size;
    return size;
}
+ (CGSize)zj_sizeOfString:(NSString *)string font:(UIFont *)font width:(CGFloat)width{
    CGSize size = [string boundingRectWithSize:CGSizeMake(width, [UIScreen mainScreen].bounds.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes: @{NSFontAttributeName:font} context:nil].size;
    return size;
}


+ (CGFloat)heightWithTextFontSize:(CGFloat)fontSize{
    CGSize size = [NSString sizeOfString:@"张" font:fontSize width:[UIScreen mainScreen].bounds.size.width];
    return size.height;
}

+ (CGFloat)heightWithTextFont:(UIFont *)font{
    CGSize size = [NSString zj_sizeOfString:@"张" font:font width:[UIScreen mainScreen].bounds.size.width];
    return size.height;
}



// 传内容string和字体大小，就可以计算多少行。
+ (NSInteger)getLineNum:(NSString*)str  font:(UIFont*)font labelWidth:(CGFloat)width
{
    if (str.length<1)
    {
        return 0;
    }
    
    CGFloat oneRowHeight = [[str substringToIndex:1] sizeWithAttributes:@{NSFontAttributeName:font}].height;
    CGSize textSize = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
    
    NSInteger rows = textSize.height / oneRowHeight;
    
    return rows;
}


//计算text的高度(带有行间距的情况)
+ (CGFloat)zj_getTextSpaceHeightFromText:(NSString *)text font:(UIFont *)font width:(CGFloat)width Space:(CGFloat)Space numberOfLines:(NSInteger)numberOfLines{
    //行数
    NSInteger row = [NSString getLineNum:text font:font labelWidth:width];
    if (numberOfLines != 0) {
        row = row > numberOfLines ? numberOfLines : row;
    }
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.alignment = NSTextAlignmentLeft;
    if (row == 1) {
        paraStyle.lineSpacing = 0.0;
    }else{
        paraStyle.lineSpacing = Space;
    }
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent = 0.0;
    paraStyle.paragraphSpacingBefore = 0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
                          };
    
    
//    CGSize size = [@"张" boundingRectWithSize:CGSizeMake(width, HEIGHT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    return [NSString heightWithTextFont:font] * row + (row - 1) * paraStyle.lineSpacing;
}


+(NSString *)getCurrentDate
{
    //获取当前时间
    NSDate *now = [NSDate date];
    
    return [NSString stringWithFormat:@"%f",[now timeIntervalSince1970]];
}

- (NSString *)zj_subStringLastOfString:(NSString *)string{
    NSMutableString *newString = [self mutableCopy];
    NSRange range;
    do {
       range = [newString rangeOfString:string];
       newString = [[newString substringFromIndex:range.location + 1] mutableCopy];
        range = [newString rangeOfString:string];
        
    } while (range.length != 0);
    

    return [NSString stringWithFormat:@"%@%@",string,newString];
}

- (NSString *)zj_subStringFirstOfString:(NSString *)string{

    NSMutableString *newString = [self mutableCopy];
    NSRange range;
    do {
        range = [newString rangeOfString:string];
        newString = [[newString substringToIndex:range.location + 1] mutableCopy];
        range = [newString rangeOfString:string];
        
    } while (range.length != 0);
    
    
    return [NSString stringWithFormat:@"%@%@",string,newString];
}

/**  计算天数  */
+ (NSString *)calculateDayFromDate:(NSString *)fromDateString toDate:(NSString *)toDateString{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    [gregorian setFirstWeekday:2];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *fromDate;
    NSDate *toDate;
    [gregorian rangeOfUnit:NSDayCalendarUnit startDate:&fromDate interval:NULL forDate:[dateFormatter dateFromString:fromDateString]];
    [gregorian rangeOfUnit:NSDayCalendarUnit startDate:&toDate interval:NULL forDate:[dateFormatter dateFromString:toDateString]];
    NSDateComponents *dayComponents = [gregorian components:NSDayCalendarUnit fromDate:fromDate toDate:toDate options:0];
    return [NSString stringWithFormat:@"%d",dayComponents.day];
    
}



+ (NSString *)getTimeLabelValues:(double)currentTime{
    double minutesElapsed = fmod(currentTime, 60);
    double secondsElapsed = floor(currentTime / 60.0) > 60.0 ? floor(currentTime / 60.0) - 60.0 : floor(currentTime / 60.0);
    double hourElapsed = floor(currentTime / 3600.0);
    return [NSString stringWithFormat:@"%02.0f:%02.0f:%02.0f",hourElapsed, secondsElapsed, minutesElapsed];
}
+ (NSArray *)getArrayFromString:(NSString *)string byString:(NSString *)byString{
    NSArray *array = [string componentsSeparatedByString:byString];
    return array;
}



//限制输入数组
+ (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

@end
